﻿using System;
using System.Configuration;
using System.Reflection;
using DbUp;
using DbUp.Helpers;

using System.Data.SqlClient;

namespace SampleDatabaseProject
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleApplicationConnectionString"].ToString());

            //using (var database = new Sqld)
            //{
                try
                {
                    //attempt to open database connection
                    con.Open();
                }
                catch
                {
                    //if it fails then create the database
                    //database.Create();
                }
                finally
                {
                    //either way, check if the connection is closed and if not, close it
                    //if (con.State != System.Data.ConnectionState.Closed)
                    //    con.Close();

                    //displose of the connection
                    //con.Dispose();
                }

                var upgrader =
                    DeployChanges.To
                    //.SqlDatabase(database.ConnectionString, null) //null or "" for default schema for user
                    .SqlDatabase(con.ConnectionString, null) //null or "" for default schema for user
                    .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                    .LogToConsole()
                    .Build();

                var result = upgrader.PerformUpgrade();

                // Display the result
                if (result.Successful)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Success!");
                    //Console.ReadKey();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(result.Error);
                    //Console.ReadKey();
                    Console.WriteLine("Failed!");
                }

                // Database will be deleted at this point
            //}

            //Console.ReadKey();
        }
    }
}
